
################################
## Tariffs and trade declines ##
################################

## tariffs + other alternatives
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat)), 
        hs10code = trdmat$hs10code, impoth = trdmat$imptop1020122016,
        imp = c(trdmat$impchn20122016/5, trdmat$impALL20122016/5, trdmat$impchn20182020/3, trdmat$impALL20182020/3)/1000,
        market = c(rep("China", nrow(trdmat)), rep("Other", nrow(trdmat)), rep("China", nrow(trdmat)), rep("Other", nrow(trdmat))),
        tar = trdmat$TWtariff,
        impothpos = c(trdmat$imptop10pos20122016, trdmat$imptop10pos20122016, trdmat$imptop10pos20122016, trdmat$imptop10pos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat$hs10code[trdmat$impchn20122016/5 > 0 | trdmat$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_tar <- newdat2
mod <- lm(log(imp+1) ~ year*market*tar + year*market*I(impothpos/10), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","tar","impothpos",
  "year_market","year_tar","market_tar",
  "year_impothpos","market_impothpos",
  "year_market_tar","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:13]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","tar_mean","impothpos_mean",
  "year_market_mean","year_tar_mean","market_tar_mean","year_impothpos_mean","market_impothpos_mean",
  "year_market_tar_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:13] <- mm_df[,2:13] - mm_df[,16:27] 

mod1tarandalt <- lm(imp ~ year + year_market + year_tar + year_market_tar + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2tarandalt <- lm(imp ~ year_tar + year_market_tar + year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

hs6codes <- substr(newdat2_tar$hs10code, 1, 6)
mods_tar <- list(coeftest(mod1tarandalt, vcov = vcovCL, cluster = hs6codes)[1:7,1:4], 
  coeftest(mod2tarandalt, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_tar[[1]])[c(5,7)])
tab <- matrix(data = NA, ncol = length(mods_tar), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_tar)){mod <- mods_tar[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Trade war tariff","Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1tarandalt)$df[1:2]),sum(summary(mod2tarandalt)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

ptable(cbind(rownames(tab), tab)[c(3:5),], "paper/tables/TableB1top.tex")

## tariffs + other alternatives (top-25)
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat)), 
        hs10code = trdmat$hs10code, impoth = trdmat$imptop1020122016,
        imp = c(trdmat$impchn20122016/5, trdmat$impALL20122016/5, trdmat$impchn20182020/3, trdmat$impALL20182020/3)/1000,
        market = c(rep("China", nrow(trdmat)), rep("Other", nrow(trdmat)), rep("China", nrow(trdmat)), rep("Other", nrow(trdmat))),
        tar = trdmat$TWtariff,
        impothpos = c(trdmat$imptop25pos20122016, trdmat$imptop25pos20122016, trdmat$imptop25pos20122016, trdmat$imptop25pos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat$hs10code[trdmat$impchn20122016/5 > 0 | trdmat$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_tar <- newdat2
mod <- lm(log(imp+1) ~ year*market*tar + year*market*log(impothpos+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","tar","impothpos",
  "year_market","year_tar","market_tar",
  "year_impothpos","market_impothpos",
  "year_market_tar","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:13]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","tar_mean","impothpos_mean",
  "year_market_mean","year_tar_mean","market_tar_mean","year_impothpos_mean","market_impothpos_mean",
  "year_market_tar_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:13] <- mm_df[,2:13] - mm_df[,16:27] 

mod1tarandalt <- lm(imp ~ year + year_market + year_tar + year_market_tar + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2tarandalt <- lm(imp ~ year_tar + year_market_tar + year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

hs6codes <- substr(newdat2_tar$hs10code, 1, 6)
mods_tar <- list(coeftest(mod1tarandalt, vcov = vcovCL, cluster = hs6codes)[1:7,1:4], 
  coeftest(mod2tarandalt, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_tar[[1]])[c(5,7)])
tab <- matrix(data = NA, ncol = length(mods_tar), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_tar)){mod <- mods_tar[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Trade war tariff","Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1tarandalt)$df[1:2]),sum(summary(mod2tarandalt)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

ptable(cbind(rownames(tab), tab)[c(3:5),], "paper/tables/TableB1mid.tex")

## tariffs + other alternatives (all)
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat)), 
        hs10code = trdmat$hs10code, impoth = trdmat$imptop1020122016,
        imp = c(trdmat$impchn20122016/5, trdmat$impALL20122016/5, trdmat$impchn20182020/3, trdmat$impALL20182020/3)/1000,
        market = c(rep("China", nrow(trdmat)), rep("Other", nrow(trdmat)), rep("China", nrow(trdmat)), rep("Other", nrow(trdmat))),
        tar = trdmat$TWtariff,
        impothpos = c(trdmat$impALLpos20122016, trdmat$impALLpos20122016, trdmat$impALLpos20122016, trdmat$impALLpos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat$hs10code[trdmat$impchn20122016/5 > 0 | trdmat$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_tar <- newdat2
mod <- lm(log(imp+1) ~ year*market*tar + year*market*log(impothpos+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","tar","impothpos",
  "year_market","year_tar","market_tar",
  "year_impothpos","market_impothpos",
  "year_market_tar","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:13]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","tar_mean","impothpos_mean",
  "year_market_mean","year_tar_mean","market_tar_mean","year_impothpos_mean","market_impothpos_mean",
  "year_market_tar_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:13] <- mm_df[,2:13] - mm_df[,16:27] 

mod1tarandalt <- lm(imp ~ year + year_market + year_tar + year_market_tar + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2tarandalt <- lm(imp ~ year_tar + year_market_tar + year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

hs6codes <- substr(newdat2_tar$hs10code, 1, 6)
mods_tar <- list(coeftest(mod1tarandalt, vcov = vcovCL, cluster = hs6codes)[1:7,1:4], 
  coeftest(mod2tarandalt, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_tar[[1]])[c(5,7)])
tab <- matrix(data = NA, ncol = length(mods_tar), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_tar)){mod <- mods_tar[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Trade war tariff","Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1tarandalt)$df[1:2]),sum(summary(mod2tarandalt)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

ptable(cbind(rownames(tab), tab)[c(3:5),], "paper/tables/TableB1bot.tex")

################################
## Exclusion requests tariffs ##
################################

## tar and alternative markets top 10
newdat <- data.frame(hs10code = trdmat$hs10code,
        impchn = c(trdmat$impchn20122016/5)/1000,
        imptop10 = c(trdmat$imptop1020122016/5)/1000,
        imptop10pos = c(trdmat$imptop10pos20122016),
        impwornonchnpos = trdmat$impALL20122016 > 0,
        impwor = c(trdmat$impALL20122016/5/1000),
        TWtariff = trdmat$TWtariff)
names(trdmat)

# merge in exclusion requestions variable
newdat <- join(newdat, excl_agg, by = "hs10code", type = "left")
newdat$numexclreq[is.na(newdat$numexclreq)] <- 0

newdat$covered <- 0
newdat$covered[substr(newdat$hs10code,1,8) %in% tars_covered$hs10code] <- 1

ana_dat <- na.omit(newdat[,c("numexclreq","TWtariff","imptop10pos","impwor","impchn","covered","hs10code")])
postrade <- trdmat$hs10code[trdmat$impchn20122016/5 > 0 | trdmat$impwor20122016/5 > 0]
ana_dat <- ana_dat[ana_dat$hs10code %in% postrade,]
mod1a_tar <- lm(log(numexclreq+1) ~ TWtariff + I(imptop10pos/10) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,2), data = ana_dat);
mod1b_tar <- lm(log(numexclreq+1) ~ TWtariff + I(imptop10pos/10) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,4), data = ana_dat); 

mod2a_tar <- lm(I(numexclreq>0) ~ TWtariff + I(imptop10pos/10) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,2), data = ana_dat); 
mod2b_tar <- lm(I(numexclreq>0) ~ TWtariff + I(imptop10pos/10) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,4), data = ana_dat); 

mods_tar <- list(coeftest(mod1a_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod1b_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod2a_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod2b_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4]) 

vars <- c(rownames(mods_tar[[1]])[c(2,3)])
tab <- matrix(data = NA, ncol = length(mods_tar), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_tar)){mod <- mods_tar[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3)] <- 
  c("Trade war tariff","Alternative markets") 
N <- c(sum(summary(mod1a_tar)$df[1:2]),sum(summary(mod1b_tar)$df[1:2]),sum(summary(mod2a_tar)$df[1:2]),sum(summary(mod2b_tar)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

ptable(cbind(rownames(tab), tab)[3:5,], "paper/tables/TableB2top.tex")

## tar and alternative markets top 25
newdat <- data.frame(hs10code = trdmat$hs10code,
        impchn = c(trdmat$impchn20122016/5)/1000,
        imptop25 = c(trdmat$imptop2520122016/5)/1000,
        imptop25pos = c(trdmat$imptop25pos20122016),
        impwornonchnpos = trdmat$impALL20122016 > 0,
        impwor = c(trdmat$impALL20122016/5/1000),
        TWtariff = trdmat$TWtariff)
names(trdmat)

# merge in exclusion requestions variable
newdat <- join(newdat, excl_agg, by = "hs10code", type = "left")
newdat$numexclreq[is.na(newdat$numexclreq)] <- 0

newdat$covered <- 0
newdat$covered[substr(newdat$hs10code,1,8) %in% tars_covered$hs10code] <- 1

ana_dat <- na.omit(newdat[,c("numexclreq","TWtariff","imptop25pos","impwor","impchn","covered","hs10code")])
postrade <- trdmat$hs10code[trdmat$impchn20122016/5 > 0 | trdmat$impwor20122016/5 > 0]
ana_dat <- ana_dat[ana_dat$hs10code %in% postrade,]
mod1a_tar <- lm(log(numexclreq+1) ~ TWtariff + log(imptop25pos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,2), data = ana_dat);
mod1b_tar <- lm(log(numexclreq+1) ~ TWtariff + log(imptop25pos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,4), data = ana_dat); 
summary(mod1a_tar)$coef[1:10,]

mod2a_tar <- lm(I(numexclreq>0) ~ TWtariff + log(imptop25pos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,2), data = ana_dat); summary(mod2a_tar)
mod2b_tar <- lm(I(numexclreq>0) ~ TWtariff + log(imptop25pos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,4), data = ana_dat); 

mods_tar <- list(coeftest(mod1a_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod1b_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod2a_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod2b_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4]) 

vars <- c(rownames(mods_tar[[1]])[c(2,3)])
tab <- matrix(data = NA, ncol = length(mods_tar), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_tar)){mod <- mods_tar[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3)] <- 
  c("Trade war tariff","Alternative markets") 
N <- c(sum(summary(mod1a_tar)$df[1:2]),sum(summary(mod1b_tar)$df[1:2]),sum(summary(mod2a_tar)$df[1:2]),sum(summary(mod2b_tar)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

ptable(cbind(rownames(tab), tab)[3:5,], "paper/tables/TableB2mid.tex")

## tar and alternative markets top 25
newdat <- data.frame(hs10code = trdmat$hs10code,
        impchn = c(trdmat$impchn20122016/5)/1000,
        impALL = c(trdmat$impALL20122016/5)/1000,
        impALLpos = c(trdmat$impALLpos20122016),
        impwornonchnpos = trdmat$impALL20122016 > 0,
        impwor = c(trdmat$impALL20122016/5/1000),
        TWtariff = trdmat$TWtariff)
names(trdmat)

# merge in exclusion requestions variable
newdat <- join(newdat, excl_agg, by = "hs10code", type = "left")
newdat$numexclreq[is.na(newdat$numexclreq)] <- 0

newdat$covered <- 0
newdat$covered[substr(newdat$hs10code,1,8) %in% tars_covered$hs10code] <- 1

ana_dat <- na.omit(newdat[,c("numexclreq","TWtariff","impALLpos","impwor","impchn","covered","hs10code")])
postrade <- trdmat$hs10code[trdmat$impchn20122016/5 > 0 | trdmat$impwor20122016/5 > 0]
ana_dat <- ana_dat[ana_dat$hs10code %in% postrade,]
mod1a_tar <- lm(log(numexclreq+1) ~ TWtariff + log(impALLpos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,2), data = ana_dat);
mod1b_tar <- lm(log(numexclreq+1) ~ TWtariff + log(impALLpos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,4), data = ana_dat); 
summary(mod1a_tar)$coef[1:10,]

mod2a_tar <- lm(I(numexclreq>0) ~ TWtariff + log(impALLpos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,2), data = ana_dat); 
mod2b_tar <- lm(I(numexclreq>0) ~ TWtariff + log(impALLpos+1) + log10(impwor+1) + log10(impchn+1) + covered + substr(hs10code,1,4), data = ana_dat); 

mods_tar <- list(coeftest(mod1a_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod1b_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod2a_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4], 
  coeftest(mod2b_tar, vcov = vcovCL, cluster = substr(ana_dat$hs10code,1,6))[1:3,1:4]) 

vars <- c(rownames(mods_tar[[1]])[c(2,3)])
tab <- matrix(data = NA, ncol = length(mods_tar), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_tar)){mod <- mods_tar[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3)] <- 
  c("Trade war tariff","Alternative markets") 
N <- c(sum(summary(mod1a_tar)$df[1:2]),sum(summary(mod1b_tar)$df[1:2]),sum(summary(mod2a_tar)$df[1:2]),sum(summary(mod2b_tar)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

ptable(cbind(rownames(tab), tab)[3:5,], "paper/tables/TableB2bot.tex")


